package com.mtyee.photoserver.fileUpload;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description
 * @Author Mtyee
 * @Date 2022-3-23 18:04
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileUpload {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUpload.class);

    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }

    @PostMapping("/upload")
    @ResponseBody
    //这段代码逻辑简单，不知道注释该写点啥，有问题可留言
    public String upload(@RequestParam("file") MultipartFile file) throws FileNotFoundException {
        String os = System.getProperty("os.name");
        LOGGER.info(os.toLowerCase());
        String fileName = file.getOriginalFilename();
        String filePath = null;
        File saveFile = null;
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        if(os.toLowerCase().startsWith("win")){
            filePath ="C:\\Users\\13303\\Desktop\\192.18.1.109\\upload\\";
        }else {
            filePath = "/www/wwwroot/192.168.1.109/upload/";
        }
        saveFile = new File(filePath+fileName);
        if (!saveFile.exists()){
            saveFile.mkdirs();
        }
        try {
            file.transferTo(saveFile);
            LOGGER.info("上传成功");
            Date d=  new Date();
            String fileType = saveFile.getName().substring(fileName.lastIndexOf("."),fileName.length());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            saveFile.renameTo(new File(filePath+sdf.format(d)+fileType));
            return filePath+sdf.format(d)+fileType;

        }catch (Exception e){
            e.printStackTrace();
        }

//上传到项目src目录下

//        if(os.toLowerCase().startsWith("win")){
//            filePath  = System.getProperty("user.dir")+"/src//main/FileData/upload/";
//        }else {
//            File rootPath = new File(ResourceUtils.getURL("classpath:").getPath());
//            if(!rootPath.exists()){
//                rootPath = new File("");
//            }
//            filePath = rootPath.getAbsolutePath()+"\\FileData\\upload\\";
//        }
//
//        File dest = new File(filePath + fileName);
//        try {
//            file.transferTo(dest);
//            LOGGER.info("上传成功");
//            Date d=  new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//            String fileType = dest.getName().substring(fileName.lastIndexOf("."),fileName.length());
//            dest.renameTo(new File(filePath+sdf.format(d)+fileType));
//            return "upload/"+sdf.format(d)+fileType;
//        } catch (IOException e) {
//            LOGGER.error(e.toString(), e);
//        }
        return "上传失败！";
    }


}

